package com.start.shop.api.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.start.shop.api.param.coupon.GetCouponByStatusParam;
import com.start.shop.api.param.coupon.GetCouponByUseableParam;
import com.start.shop.api.param.coupon.IssueCouponParam;
import com.start.shop.api.vo.coupon.CouponVo;
import com.start.shop.api.vo.coupon.CouponVoPage;
import com.start.shop.common.constant.MsgObject;
import com.start.shop.common.constant.RspCode;
import com.start.shop.common.constant.TableFeildCode;
import com.start.shop.modules.repository.entity.Coupon;
import com.start.shop.modules.repository.entity.CouponConfig;
import com.start.shop.modules.repository.entity.Goods;
import com.start.shop.modules.service.ICouponConfigService;
import com.start.shop.modules.service.ICouponService;
import com.start.shop.modules.service.IGoodsService;

import lombok.extern.slf4j.Slf4j;




@Slf4j
@RestController
@RequestMapping(value = "/coupon")
public class CouponCtroller {

	private Logger logger = LoggerFactory.getLogger(CouponCtroller.class);
	
	@Autowired
	ICouponService couponService;
	
	@Autowired
	ICouponConfigService couponConfigService;
	
	@Autowired
	IGoodsService goodsService;


	/**
	 * showdoc
	 * 
	 * @catalog 服务功能/优惠券服务
	 * @title 优惠券发放(issue)
	 * @description 优惠券发放
	 * @method post
	 * @url https://www.parpur.com/coupon/v1/issue
	 * @param custId true String 客户编号
	 * @param couponSortId true int 优惠券种类
	 * @param num true int 发放数量
	 * @return {"code":"错误码","msg":"错误描述"}
	 * @return_param code string 错误码
	 * @return_param msg string 错误描述
	 * @remark 
	 * @number 1
	 */
	@RequestMapping(value = "/v1/issue")
	public  ResponseEntity<MsgObject>  issueCoupon(@RequestBody @Valid IssueCouponParam issueCouponParam) {
		//查询优惠券种类
		CouponConfig couponConfig = couponConfigService.getCouponConifgById(issueCouponParam.getCouponSortId());
		if (couponConfig == null) {
			return ResponseEntity.ok(MsgObject.fail(RspCode.COUPON_NOT_FOUND));
		}
		
		//检查发行的优惠券状态
		if (TableFeildCode.T_COUPONCONFIG_STATUS_NOT_NOMAL.getCode().equals(String.valueOf(couponConfig.getStatus()))) {
			log.error("该优惠券种类已经下架");
			return ResponseEntity.ok(MsgObject.fail(RspCode.COUPON_STOCK_OUT_DATE));
		}
		
		//扣减优惠券库存
		couponConfigService.couponStockOut(couponConfig.getSortId(), issueCouponParam.getNum());
		
		//添加优惠券
		Coupon coupon = IssueCouponParam.IssueCouponParamToCoupon(issueCouponParam, couponConfig);
		couponService.issueCoupon(coupon);
		
		//返回结果
		return ResponseEntity.ok(MsgObject.success(null));
	}


	/**
	 * showdoc
	 * 
	 * @catalog 服务功能/优惠券服务
	 * @title 查询可用优惠券(getUseableCoupon)
	 * @description 查询优惠券可用列表
	 * @method post
	 * @url https://www.parpur.com/coupon/v1/getUseableCoupon
	 * @param custId true String 客户编号
	 * @param orderAmt true int 订单金额
	 * @param goodsId true int 商品编号
	 * @param goodsType true int 商品类型
	 * @return {"code":"错误码","msg":"错误描述", "data":[{couponId: 1, sortId: 1, couponName: "满100元减20元", couponCondition: "100", conditionAmt: 1000,…},…]}
	 * @return_param code string 错误码
	 * @return_param msg string 错误描述
	 * @return_param data List<Coupon> 优惠券
	 * @remark ```Coupon{couponId:xxx,sortId:xxx,couponName:xxx,couponCondition:xxx,conditionAmt:xxx,conditionGoodsId:xxx,conditionGoodsType:xxx,cutAmt:xxx,couponDesc:xxx,couponCanUse:xxx,couponStatus:xxx,startDate:xxx,endDate:xxx,createDate:xxx} ```
	 * @number 2
	 */
	@RequestMapping(value = "/v1/getCouponByUseable")
	public  ResponseEntity<MsgObject>  getUseableCoupon(@RequestBody @Valid GetCouponByUseableParam getCouponByUseableParam) {
		//获取商品信息
		PageInfo<Coupon> couponPage = null;
		Goods goods = goodsService.getGoodsByGoodsId(getCouponByUseableParam.getGoodsId());
		if (getCouponByUseableParam.getStatus()==1) {
			couponPage = couponService.getUseableCoupon(getCouponByUseableParam.getUser().getUserId(), 
					getCouponByUseableParam.getOrderAmt(), 
					goods.getGoodsId(),goods.getGoodsType(),
					getCouponByUseableParam.getPageNum(),
					getCouponByUseableParam.getPageSize());
		}
		else if (getCouponByUseableParam.getStatus()==0) {
			couponPage = couponService.getDisableCoupon(getCouponByUseableParam.getUser().getUserId(), 
					getCouponByUseableParam.getOrderAmt(), 
					goods.getGoodsId(),goods.getGoodsType(),
					getCouponByUseableParam.getPageNum(),
					getCouponByUseableParam.getPageSize());
		}
		else {
			return ResponseEntity.ok(MsgObject.fail(RspCode.FAIL));
		}
			
		// 转换coupon输出格式
		List<CouponVo> couponVoList = new ArrayList<CouponVo>() ;
		for (Coupon coupon : couponPage.getList()) {
			CouponConfig couponConfig = couponConfigService.getCouponConifgById(coupon.getSortId());
			CouponVo couponVo = CouponVo.setData(coupon, couponConfig);
			couponVoList.add(couponVo);
		}
        //组织结果
        CouponVoPage couponVoPage = CouponVoPage.setData(couponVoList, couponPage);
		
		//返回结果
		return ResponseEntity.ok(MsgObject.success(couponVoPage));
	}
	
	
	/**
	 * showdoc
	 * 
	 * @catalog 服务功能/优惠券服务
	 * @title 查询所有优惠券(getAllCoupon)
	 * @description 查询所有优惠券
	 * @method post
	 * @url https://www.parpur.com/coupon/v1/getAllCoupon
	 * @param custId true String 客户编号
	 * @return {"code":"错误码","msg":"错误描述", "data":[{couponId: 1, sortId: 1, couponName: "满100元减20元", couponCondition: "100", conditionAmt: 1000,…},…]}
	 * @return_param code string 错误码
	 * @return_param msg string 错误描述
	 * @return_param data List<Coupon> 优惠券
	 * @remark ```Coupon{couponId:xxx,sortId:xxx,couponName:xxx,couponCondition:xxx,conditionAmt:xxx,conditionGoodsId:xxx,conditionGoodsType:xxx,cutAmt:xxx,couponDesc:xxx,couponCanUse:xxx,couponStatus:xxx,startDate:xxx,endDate:xxx,createDate:xxx} ```
	 * @number 3
	 */
	@RequestMapping(value = "/v1/getCouponByStatus")
	public  ResponseEntity<MsgObject>  getCouponByStatus(@RequestBody @Valid GetCouponByStatusParam getCouponByStatusParam) {
	    //获取客户所有的优惠券
		PageInfo<Coupon> couponPage= couponService.getCouponsByStatus(
										getCouponByStatusParam.getUser().getUserId(),
										getCouponByStatusParam.getStatus(),
										getCouponByStatusParam.getPageNum(), 
										getCouponByStatusParam.getPageSize()
									);
		//将优惠券转换为对外的优惠券信息
        List<CouponVo> couponVoList = new ArrayList<CouponVo>();
        for (Coupon coupon : couponPage.getList()) {
        	CouponVo couponVo = CouponVo.setData(coupon, couponConfigService.getCouponConifgById(coupon.getSortId()));
        	couponVoList.add(couponVo);
		}
        
        //组织结果
        CouponVoPage couponVoPage = CouponVoPage.setData(couponVoList, couponPage);
		
        //返回结果
		return ResponseEntity.ok(MsgObject.success(couponVoPage));
	}
}
